/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Interface;

import javax.swing.JOptionPane;

/**
 *
 * @author Nordahl
 */
public class WelcomeDA extends javax.swing.JPanel {
    tvmGUI master;
    CartDA CartDAClass;
    /**
     * Creates new form WelcomeDA
     */
    public WelcomeDA(tvmGUI tvmGUI) {
        master = tvmGUI;
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        LabWelcomeDATitle = new javax.swing.JLabel();
        LabWelcomeDAInstruct = new javax.swing.JLabel();
        LabWelcomeDAInfo = new javax.swing.JLabel();
        LabWelcomeDATime = new javax.swing.JLabel();
        CBWelcomeDAType = new javax.swing.JComboBox();
        LabWelcomeDAPricePZ = new javax.swing.JLabel();
        CBWelcomeDAZone = new javax.swing.JComboBox();
        LabWelcomeDATypeError = new javax.swing.JLabel();
        LabWelcomeDAZonesError = new javax.swing.JLabel();
        LabWelcomeDAError = new javax.swing.JLabel();
        CBWelcomeDAAmount = new javax.swing.JComboBox();
        LabWelcomeDATempPrice = new javax.swing.JLabel();
        LabWelcomeDATotalPrice = new javax.swing.JLabel();
        LabWelcomeDAAmountError = new javax.swing.JLabel();
        ButWelcomeDANext = new javax.swing.JButton();
        ButWelcomeDAHelp = new javax.swing.JButton();
        ButWelcomeDALang = new javax.swing.JButton();
        BackgroundPIC = new javax.swing.JLabel();

        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        LabWelcomeDATitle.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        LabWelcomeDATitle.setText("Velkommen til BlueJ Trafikselskab");
        add(LabWelcomeDATitle, new org.netbeans.lib.awtextra.AbsoluteConstraints(63, 69, -1, -1));

        LabWelcomeDAInstruct.setText("Vælg billettype, antal zoner og antal billetter fra dropdown menuerne herunder og tryk næste.");
        add(LabWelcomeDAInstruct, new org.netbeans.lib.awtextra.AbsoluteConstraints(63, 116, -1, -1));

        LabWelcomeDAInfo.setText(" ");
        add(LabWelcomeDAInfo, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 11, 193, -1));

        LabWelcomeDATime.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        LabWelcomeDATime.setText(" ");
        add(LabWelcomeDATime, new org.netbeans.lib.awtextra.AbsoluteConstraints(565, 11, 225, -1));

        CBWelcomeDAType.setMaximumRowCount(10);
        CBWelcomeDAType.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Vælg billettype..." }));
        CBWelcomeDAType.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CBWelcomeDATypeActionPerformed(evt);
            }
        });
        add(CBWelcomeDAType, new org.netbeans.lib.awtextra.AbsoluteConstraints(63, 171, 126, -1));

        LabWelcomeDAPricePZ.setText(" ");
        add(LabWelcomeDAPricePZ, new org.netbeans.lib.awtextra.AbsoluteConstraints(196, 174, 135, -1));

        CBWelcomeDAZone.setMaximumRowCount(10);
        CBWelcomeDAZone.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Vælg antal zoner...", "2-zoner", "3-zoner", "4-zoner", "5-zoner", "6-zoner", "7-zoner", "Alle-zoner" }));
        CBWelcomeDAZone.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CBWelcomeDAZoneActionPerformed(evt);
            }
        });
        add(CBWelcomeDAZone, new org.netbeans.lib.awtextra.AbsoluteConstraints(337, 171, -1, -1));

        LabWelcomeDATypeError.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
        LabWelcomeDATypeError.setForeground(new java.awt.Color(255, 0, 0));
        LabWelcomeDATypeError.setText(" ");
        add(LabWelcomeDATypeError, new org.netbeans.lib.awtextra.AbsoluteConstraints(63, 151, 96, -1));

        LabWelcomeDAZonesError.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
        LabWelcomeDAZonesError.setForeground(new java.awt.Color(255, 0, 0));
        LabWelcomeDAZonesError.setText(" ");
        add(LabWelcomeDAZonesError, new org.netbeans.lib.awtextra.AbsoluteConstraints(337, 151, 110, -1));

        LabWelcomeDAError.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
        LabWelcomeDAError.setForeground(new java.awt.Color(255, 0, 0));
        LabWelcomeDAError.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        LabWelcomeDAError.setText(" ");
        add(LabWelcomeDAError, new org.netbeans.lib.awtextra.AbsoluteConstraints(288, 341, 242, -1));

        CBWelcomeDAAmount.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Vælg antal billetter...", "1 stk", "2 stk", "3 stk", "4 stk", "5 stk" }));
        CBWelcomeDAAmount.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CBWelcomeDAAmountActionPerformed(evt);
            }
        });
        add(CBWelcomeDAAmount, new org.netbeans.lib.awtextra.AbsoluteConstraints(605, 171, -1, -1));

        LabWelcomeDATempPrice.setText("Pris pr. billet: ");
        add(LabWelcomeDATempPrice, new org.netbeans.lib.awtextra.AbsoluteConstraints(63, 408, 200, -1));

        LabWelcomeDATotalPrice.setText("Total pris:");
        add(LabWelcomeDATotalPrice, new org.netbeans.lib.awtextra.AbsoluteConstraints(63, 433, 200, -1));

        LabWelcomeDAAmountError.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
        LabWelcomeDAAmountError.setForeground(new java.awt.Color(255, 0, 0));
        LabWelcomeDAAmountError.setText(" ");
        add(LabWelcomeDAAmountError, new org.netbeans.lib.awtextra.AbsoluteConstraints(605, 151, 127, -1));

        ButWelcomeDANext.setText("Næste");
        ButWelcomeDANext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButWelcomeDANextActionPerformed(evt);
            }
        });
        add(ButWelcomeDANext, new org.netbeans.lib.awtextra.AbsoluteConstraints(704, 550, 86, 32));

        ButWelcomeDAHelp.setText("?");
        ButWelcomeDAHelp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButWelcomeDAHelpActionPerformed(evt);
            }
        });
        add(ButWelcomeDAHelp, new org.netbeans.lib.awtextra.AbsoluteConstraints(131, 551, -1, 31));

        ButWelcomeDALang.setText("[LANGUAGE]");
        add(ButWelcomeDALang, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 550, -1, 32));

        BackgroundPIC.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Interface/BackgroundGUI.png"))); // NOI18N
        add(BackgroundPIC, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 810, 600));
    }// </editor-fold>//GEN-END:initComponents
    
    public void setReferences(CartDA refCartDA)
    {    
        CartDAClass = refCartDA;
    }
    
    private void CBWelcomeDATypeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CBWelcomeDATypeActionPerformed
        //Checking which item is selected and setting price accordingly
        int n = CBWelcomeDAType.getItemCount()-1;   //Begin from index 0
        for (int m = 0; m<n; m++)
        {
            String type = master.CT.transferTicket(m).getTypeDA();
            if (type.equals(CBWelcomeDAType.getSelectedItem().toString()))
            {
                master.typePricePZ = master.CT.transferTicket(m).getPricePerZone();
                LabWelcomeDAPricePZ.setText("Pris pr. zone: "+master.typePricePZ+" DKK");
            }
        }
        if (CBWelcomeDAType.getSelectedItem().toString().equals("Vælg billettype..."))
        {
            LabWelcomeDAError.setText("Fejl - Et eller flere valg er ugyldige");
            LabWelcomeDATypeError.setText("Ugyldigt valg");
            LabWelcomeDAPricePZ.setText(" ");
            LabWelcomeDATempPrice.setText("Pris pr. billet: ");
            LabWelcomeDATotalPrice.setText("Total pris: ");
        }
        else
        {
            LabWelcomeDAError.setText(" ");
            LabWelcomeDATypeError.setText(" ");
            LabWelcomeDAPricePZ.setVisible(true);
            if (!CBWelcomeDAType.getSelectedItem().toString().equals("Vælg billettype...") &&
                !CBWelcomeDAZone.getSelectedItem().toString().equals("Vælg antal zoner...") &&
                !CBWelcomeDAAmount.getSelectedItem().toString().equals("Vælg antal billetter...")
            ) {
                LabWelcomeDATotalPrice.setText("Total pris: "+(master.typePricePZ*master.amountZones*master.amountTickets)+" DKK");
            }
            if (!CBWelcomeDAType.getSelectedItem().toString().equals("Vælg billettype...") &&
                !CBWelcomeDAZone.getSelectedItem().toString().equals("Vælg antal zoner...")
            ) {
                LabWelcomeDATempPrice.setText("Pris pr. billet: "+(master.typePricePZ*master.amountZones)+" DKK");
            }
        }
    }//GEN-LAST:event_CBWelcomeDATypeActionPerformed

    private void CBWelcomeDAZoneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CBWelcomeDAZoneActionPerformed
        if (CBWelcomeDAZone.getSelectedItem().toString().equals("Vælg antal zoner...")) {
            LabWelcomeDAError.setText("Fejl - Et eller flere valg er ugyldige");
            LabWelcomeDAZonesError.setText("Ugyldigt valg");
            LabWelcomeDATempPrice.setText("Pris pr. billet: ");
            LabWelcomeDATotalPrice.setText("Total pris: ");
        } else {
            try {
                master.amountZones =  Integer.parseInt(CBWelcomeDAZone.getSelectedItem().toString().substring(0, 1));
            } catch (NumberFormatException e) {
                master.amountZones = 8;
            }
            LabWelcomeDAError.setText(" ");
            LabWelcomeDAZonesError.setText(" ");
            if (!CBWelcomeDAType.getSelectedItem().toString().equals("Vælg billettype...") &&
                !CBWelcomeDAZone.getSelectedItem().toString().equals("Vælg antal zoner...") &&
                !CBWelcomeDAAmount.getSelectedItem().toString().equals("Vælg antal billetter...")
            ) {
                LabWelcomeDATotalPrice.setText("Total pris: "+(master.typePricePZ*master.amountZones*master.amountTickets)+" DKK");
            }
            if (!CBWelcomeDAType.getSelectedItem().toString().equals("Vælg billettype...") &&
                !CBWelcomeDAZone.getSelectedItem().toString().equals("Vælg antal zoner...")
            ) {
                LabWelcomeDATempPrice.setText("Pris pr. billet: "+(master.typePricePZ*master.amountZones)+" DKK");
            }
        }
        if (!CBWelcomeDAType.getSelectedItem().toString().equals("Vælg billettype...") && !CBWelcomeDAZone.getSelectedItem().toString().equals("Vælg antal zoner...")) {
            LabWelcomeDATempPrice.setText("Pris pr. billet: "+(master.typePricePZ*master.amountZones)+" DKK");
        }
    }//GEN-LAST:event_CBWelcomeDAZoneActionPerformed

    private void CBWelcomeDAAmountActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CBWelcomeDAAmountActionPerformed
        if (CBWelcomeDAAmount.getSelectedItem().toString().equals("Vælg antal billetter...")) {
            LabWelcomeDAError.setText("Fejl - Et eller flere valg er ugyldige");
            LabWelcomeDAAmountError.setText("Ugyldigt valg");
            LabWelcomeDATotalPrice.setText("Total pris: ");
        } else {
            master.amountTickets = Integer.parseInt(CBWelcomeDAAmount.getSelectedItem().toString().substring(0, 1));
            LabWelcomeDAError.setText(" ");
            LabWelcomeDAAmountError.setText(" ");
        }
        if (!CBWelcomeDAType.getSelectedItem().toString().equals("Vælg billettype...") &&
            !CBWelcomeDAZone.getSelectedItem().toString().equals("Vælg antal zoner...") &&
            !CBWelcomeDAAmount.getSelectedItem().toString().equals("Vælg antal billetter...")
        ) {
            LabWelcomeDATotalPrice.setText("Total pris: "+(master.typePricePZ*master.amountZones*master.amountTickets)+" DKK");
        }
    }//GEN-LAST:event_CBWelcomeDAAmountActionPerformed

    @SuppressWarnings("unchecked")
    private void ButWelcomeDANextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButWelcomeDANextActionPerformed
        int ticketIndex = 0;
        int n = master.CT.getArray().size()+2;  //Array begins from 0; ComboWindo begins from 1 with first being unusable

        if (CBWelcomeDAType.getSelectedItem().toString().equals("Vælg billettype...") ||
            CBWelcomeDAZone.getSelectedItem().toString().equals("Vælg antal zoner...") ||
            CBWelcomeDAAmount.getSelectedItem().toString().equals("Vælg antal billetter...")
        ) {
            return;
        }

        for (int m = 0; m<n; m++)
        {
            String type = master.CT.transferTicket(m).getTypeDA();
            if (type.equals(CBWelcomeDAType.getSelectedItem().toString()))
            {
                ticketIndex = m;
                break;
            }
        }
        master.SB.addToCart(ticketIndex, master.amountZones, CBWelcomeDAAmount.getSelectedIndex());
        // add selected ticket to soppingcart
        String toListAmount = master.amountTickets+" stk.";
        String toListType = master.CT.transferTicket(ticketIndex).getTypeDA();
        String toListZones = master.amountZones+"-zoner";
        // Check if last option is selected
        String temp_s = ""+CBWelcomeDAZone.getSelectedItem();
        int selectedAmountZones = 0;
        if (temp_s.equals("Alle-zoner")) {
            selectedAmountZones = 8;       // All zoner
        } else {    // If not, substring to first char to ignore " stk"
            selectedAmountZones = Integer.parseInt(temp_s.substring(0,1));
        }
        String toListSinglePrice = (master.CT.transferTicket(ticketIndex).getPricePerZone()*selectedAmountZones)+" DKK";

        // Separate variable to calculate total price
        int subTotalCalc = master.typePricePZ*master.amountZones*master.amountTickets;
        String toListsubTotal = subTotalCalc+" DKK";

        //Convert type length to maximum 8 characters
        if(toListType.length() > 9) {
            toListType = toListType.substring(0, 8);
        }
        //Fit to list design with 20 characters in total
        toListAmount = master.fitToListCart(toListAmount);
        toListType = master.fitToListCart(toListType);
        toListZones = master.fitToListCart(toListZones);
        toListSinglePrice = master.fitToListCart(toListSinglePrice);

        // Add selected ticket to an array and display the array on a list on next window
        master.CartContent.add(toListAmount+toListType+toListZones+toListSinglePrice+toListsubTotal);
        CartDAClass.ListCartDATicketList.setListData(master.CartContent.toArray());

        // Show next window
        master.ChangePanel(CartDAClass);
        CartDAClass.LabCartDAInfo.setText("Maskin ID: "+master.hardID+"  Zone: "+master.startZone);
        CartDAClass.LabCartDATotal.setText("Total pris: "+master.SB.getTotalPrice()+" DKK");
    }//GEN-LAST:event_ButWelcomeDANextActionPerformed

    private void ButWelcomeDAHelpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButWelcomeDAHelpActionPerformed
        JOptionPane.showMessageDialog(this,
            "\nFor hjælp ring xx xx xx xx"
            + "\nFor teknisk assistance ring xx xx xx xx"
            + "\nHusk at oplyse maskinens ID, som kan findes i øvre venstre hjørne."
            + "\n"
            + "\nBlueJ Trakfikselskab","Hjælp og teknisk support",
            JOptionPane.INFORMATION_MESSAGE);
    }//GEN-LAST:event_ButWelcomeDAHelpActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    public javax.swing.JLabel BackgroundPIC;
    public javax.swing.JButton ButWelcomeDAHelp;
    public javax.swing.JButton ButWelcomeDALang;
    public javax.swing.JButton ButWelcomeDANext;
    public javax.swing.JComboBox CBWelcomeDAAmount;
    public javax.swing.JComboBox CBWelcomeDAType;
    public javax.swing.JComboBox CBWelcomeDAZone;
    public javax.swing.JLabel LabWelcomeDAAmountError;
    public javax.swing.JLabel LabWelcomeDAError;
    public javax.swing.JLabel LabWelcomeDAInfo;
    public javax.swing.JLabel LabWelcomeDAInstruct;
    public javax.swing.JLabel LabWelcomeDAPricePZ;
    public javax.swing.JLabel LabWelcomeDATempPrice;
    public javax.swing.JLabel LabWelcomeDATime;
    public javax.swing.JLabel LabWelcomeDATitle;
    public javax.swing.JLabel LabWelcomeDATotalPrice;
    public javax.swing.JLabel LabWelcomeDATypeError;
    public javax.swing.JLabel LabWelcomeDAZonesError;
    // End of variables declaration//GEN-END:variables
}
